// 效验方法汇总

// 效验手机号
export function phoneReg(val){
    let reg = /^1(3|4|5|6|7|8|9)\d{9}$/
    return reg.test(val)
}

//密码校验
export function passwordReg(val){
    let reg = new RegExp("^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)[A-Za-z\\d!@#$%^&*()_+\\-={}\\[\\]:\";'<>?,./]*$")
    return reg.test(val)
}
//长度为8-16位
export function lengthReg(val){
  let reg =  /^.{8,16}$/;
  return reg.test(val)
}

//用户名校验
export function userNameReg(val){
    let reg = new RegExp("^[A-Za-z0-9][A-Za-z0-9_-]{3,}$");
    return reg.test(val)
}

// 检验联系方式
export function phoneMobilesReg(val){
    let reg = /^(0|86|17951)?(13[0-9]|15[012356789]|17[0135678]|18[0-9]|19[0-9]|14[57])[0-9]{8}$/;
    return reg.test(val)
}

// 检验手机固话
export function mobiles(val){
    let reg = /^((0\d{2,3}-\d{7,8})|(1[3584]\d{9}))$/;
    return reg.test(val)
}

//邮箱正则验证
export function isEmail(obj) {
    let reg = new RegExp("^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$"); //正则表达式
    return reg.test(obj)
}

//统一社会信用代码正则验证
export function isCreditCode(obj){
    let reg = /^[0-9A-HJ-NPQRTUWXY]{2}\d{6}[0-9A-HJ-NPQRTUWXY]{10}$/; //正则表达式
    return reg.test(obj)
}

//身份证正则校验
export function isCardID(sId) {
    let aCity = { 11: "北京", 12: "天津", 13: "河北", 14: "山西", 15: "内蒙古", 21: "辽宁", 22: "吉林", 23: "黑龙江", 31: "上海", 32: "江苏", 33: "浙江", 34: "安徽", 35: "福建", 36: "江西", 37: "山东", 41: "河南", 42: "湖北", 43: "湖南", 44: "广东", 45: "广西", 46: "海南", 50: "重庆", 51: "四川", 52: "贵州", 53: "云南", 54: "西藏", 61: "陕西", 62: "甘肃", 63: "青海", 64: "宁夏", 65: "新疆", 71: "台湾", 81: "香港", 82: "澳门", 91: "国外" }
    let iSum = 0;
    let info = "";
    // if ( !new RegExp(regexEnum.idcard).test(sId)) return "你输入的身份证长度或格式错误";
    let slen = sId.length;
    let reg2 = /([a-zA-Z0-9]{15,17})/;
    let reg = /(^\d{17}(\d|X)$)/;
    if (slen > 9) {
        if (reg.test(sId) === false)
            //      return "你输入的身份证长度或格式错误,若最后一位为X请输入大写";
            return "身份证长度或格式错误,若最后一位为X请输入大写";
        sId = sId.replace(/x$/i, "a");

        if (aCity[parseInt(sId.substr(0, 2))] == null) return "您的身份证地区非法";
        let sBirthday = sId.substr(6, 4) + "-" + Number(sId.substr(10, 2)) + "-" + Number(sId.substr(12, 2));
        let d = new Date(sBirthday.replace(/-/g, "/"));
        if (sBirthday != (d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate())) return "身份证上的出生日期非法";
        for (let i = 17; i >= 0; i--) iSum += (Math.pow(2, i) % 11) * parseInt(sId.charAt(17 - i), 11);
        if (iSum % 11 != 1) return "您输入的身份证号非法";
        return true;//aCity[parseInt(sId.substr(0,2))]+","+sBirthday+","+(sId.substr(16,1)%2?"男":"女")
    } else {
        if (reg2.test(sId) === false) {
            return "身份证格式有误";
        } else {
            return true;
        }
    }
}


// 整数
export function isInteger(obj){
    let reg = new RegExp("^-?\\d+$")
    return reg.test(obj)
}

// 正整数
export function isPositiveInteger(obj){
    let reg = new RegExp("^\\d+$")
    return reg.test(obj)
}

// 最多两位小数（正负都可）
export function isFloat(obj){
    let reg = /^-?\d+(\.\d{1,2})?$/
    return reg.test(obj)
}

// 最多两位小数(正数)
export function isPositiveFloat(obj){
    let reg = /^\d+(\.\d{1,2})?$/
    return reg.test(obj)
}

// 百分比(最多两位小数)
export function isPercentage(value) {
    // 正则表达式：匹配数字和百分号（%）组成的字符串
    var regex = /^\d+\.?\d{0,2}%$/;
    return regex.test(value);
}
/*
https://www.cnblogs.com/czhowe/p/17544303.html
使用案例：
    <template>
        <el-form :model="form" >
            <el-form-item label="测试" prop="demo"
            rules="[{ required: true, message: '请输入父id', trigger: 'blur'},
                { validator: demoCheck, trigger: ['blur', 'change'] }]">
                <el-input v-model="form.demo" placeholder="请输入"></el-input>
            </el-form-item>
        </el-form>
    </template>
    <script>
        import {isInteger} from '@/utils/validation'
        export default{
            data(){
                var demoCheck = (rule, value, callback) => {
                    var result =  isFloat(value)
                    if(result === true){
                        callback();
                    }else if(result === false){
                        // 报错直接返回提示语
                        callback(new Error('请输入正确的格式'));
                    }else{
                        // 身份证的校验回返回错误原因，需要返回结果
                        callback(new Error(result));
                    }
                }
                return{
                    demoCheck,
                }
            }
        }
    </script>

*/
/*
element rules 自带校验
whitespace:true::只输入空格无效，依然会被判定为空，不添加这个属性，添加空格判定有效
{ type: 'string', message: '请输入字符串类型（默认值）',whitespace:true, trigger: ['blur', 'change'] }
{ type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
{ type: 'url', message: '请输入正确的网址', trigger: ['blur', 'change'] }
{ type: 'number', message: '请输入正确的数字类型', trigger: ['blur', 'change'] }
{ type: 'boolean', message: '请输入正确的布尔类型', trigger: ['blur', 'change'] }
{ type: 'method', message: '请输入正确的函数类型', trigger: ['blur', 'change'] }
{ type: 'regexp', message: '请输入正确的正则表达式', trigger: ['blur', 'change'] }
{ type: 'integer', message: '请输入正确的整型', trigger: ['blur', 'change'] }
{ type: 'float', message: '请输入正确的双精度浮点型数字', trigger: ['blur', 'change'] }
{ type: 'array', message: '请输入正确的数组类型', trigger: ['blur', 'change'] }
{ type: 'object', message: '请输入正确的对象类型', trigger: ['blur', 'change'] }
{ type: 'enum', message: '请输入正确的枚举值', trigger: ['blur', 'change'] }
{ type: 'date', message: '请输入正确的日期格式', trigger: ['blur', 'change'] }
{ type: 'hex', message: '请输入正确的16进制数字', trigger: ['blur', 'change'] }
{ type: 'any', message: '请输入任意类型', trigger: ['blur', 'change'] }


*/


